package com.yfuxdeer.youchat.dao.impl;

import com.yfuxdeer.youchat.dao.UserDao;
import com.yfuxdeer.youchat.pojo.User;
import com.yfuxdeer.youchat.utils.Store;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Random;


public class UserDaoImpl implements UserDao {

    JdbcTemplate template = new JdbcTemplate(Store.getDataSource());

    @Override
    public User queryByUsernameAndPassword(String username, String password) {
        System.out.println(username + " : " + password);
        List<User> userList = template.query("select * from y_user " +
                        "where (username = ? or phone = ? or email= ? ) and password=?",
                new BeanPropertyRowMapper<User>(User.class), new Object[]{username, username, username, password});
        return userList.isEmpty() ? null : userList.get(0);
    }

    @Override
    public User findUserById(String user_id) {
        List<User> userList = template.query("select * from y_user where username=?",
                new BeanPropertyRowMapper<User>(User.class), new Object[]{user_id});
        return userList.isEmpty() ? null : userList.get(0);
    }

    @Override
    public void update(User user) {
        template.update("update y_user\n" +
                "set figure=?,\n" +
                "    nickName=?,\n" +
                "    gender=?,\n" +
                "    age=?,\n" +
                "    birthday=?,\n" +
                "    constellation=?,\n" +
                "    school=?,\n" +
                "    address=?,\n" +
                "    occupation=?,\n" +
                "    email=?,\n" +
                "    phone=?\n" +
                "where username = ?", new Object[]{
                user.getFigure(), user.getNickName(), user.getGender(), user.getAge(),
                user.getBirthday(), user.getConstellation(), user.getSchool(), user.getAddress(),
                user.getOccupation(), user.getEmail(), user.getPhone(), user.getUsername()
        });
    }

    @Override
    public void updatePassword(String username, String password) throws Exception {
        template.update("update y_user\n" +
                "set password=? \n" +
                "where username = ?", new Object[]{
                password, username
        });
    }

    @Override
    public void insert(User user) {
        template.update("insert into y_user(username,password,email,phone,nickName) values(?,?,?,?,?)",
                new Object[]{user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(),
                        "YC"+(new Random().nextInt(89_999_999)+10_000_000)});
    }

    @Override
    public List<User> searchUserByInfo(String username, String info) {
        return template.query("select *\n" +
                        "from y_user\n" +
                        "where username <> ?\n" +
                        "and (username = ?\n" +
                        "or nickName = ?" +
                        "or phone = ?" +
                        "or email = ?)",
                new BeanPropertyRowMapper<>(User.class),
                new Object[]{username, info, info, info, info});
    }

    @Override
    public User findUserByPhoneOrEmail(String info) {
        List<User> userList = template.query("select * from y_user where phone=? or email=?",
                new BeanPropertyRowMapper<User>(User.class), new Object[]{info, info});
        return userList.isEmpty() ? null : userList.get(0);
    }
}
